<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script th:src="@{/static/layui/layui.js}" charset="utf-8"></script>
    <link rel="stylesheet" th:href="@{/static/layui/css/public.css}" media="all">
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}" media="all">
    <script type="text/javascript" th:src="@{/static/layui/jquery.min.js}"></script>
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">姓名</label>
        <div class="layui-input-block">
            <input type="text" name="uname" lay-verify="required" lay-reqtext="姓名不能为空" placeholder="请输入姓名" value="" class="layui-input">
            <tip>填写自己的姓名。</tip>
        </div>
    </div>
    <!--    <div class="layui-form-item">-->
    <!--        <label class="layui-form-label required">性别</label>-->
    <!--        <div class="layui-input-block">-->
    <!--            <input type="radio" name="sex" value="男" title="男" checked="">-->
    <!--            <input type="radio" name="sex" value="女" title="女">-->
    <!--        </div>-->
    <!--    </div>-->
    <div class="layui-form-item">
        <label class="layui-form-label required">照片名字</label>
        <div class="layui-input-block">
            <input type="text" name="pname" lay-verify="required"  class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <!--        <label class="layui-form-label">照片</label>-->
        <!--        <div class="layui-input-block">-->
        <!--&lt;!&ndash;            <input type="file" name="path" value="" class="layui-input">&ndash;&gt;-->
        <!--            <button type="button" class="layui-btn" id="test1" name="path">上传图片</button>-->
        <!--        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label">上传照片</label>
            <div class="layui-input-inline uploadHeadImage">
                <div class="layui-upload-drag" id="headImg" accept="image/jpeg, image/png">
                    <i class="layui-icon"></i>
                    <p>点击上传图片，或将图片拖拽到此处</p>
                </div>
            </div>
            预览图：
            <div class="layui-upload-list" id="demo2"></div>
            <input type="text" id="imgUrls" name="imgUrls" accept="image/jpeg, image/png" style="display: none;" class="layui-input">
            <!--           文件类型只能是word，ppt，pdf，jpg，png中的一种 ！且大小不能超过 10 M。-->
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>

<script>

    $(function () {
        layui.use(["jquery", "upload", "form", "layer", "element"], function () {
            var $ = layui.$,
                element = layui.element,
                layer = layui.layer,
                upload = layui.upload,
                form = layui.form;
            var s = "";

            //拖拽上传
            var uploadInst = upload.render({
                elem: '#headImg'
                , url: 'http://localhost:8080/images/upload'
                , size: 100000   //上传文件大小  单位KB
                , multiple: true
                ,accept:'file'
                , number: 1    //上传图片的数量
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        // $('#demo1').attr('src', result); //图片链接（base64）
                        $('#demo2').append('<img src="' + result
                            + '" alt="' + file.name
                            + '"height="100px" width="100px" class="layui-upload-img uploadImgPreView">')


                    });
                }
                , done: function (res) {
                    if (s == "") {
                        s = JSON.stringify(res);
                    } else {
                        s = s + "," + JSON.stringify(res);
                    };
                    localStorage.setItem("address1", s);
                }
                , error: function () {
                    alert("图片上传错误");
                }

            });

            //监听提交
            form.on('submit(saveBtn)', function (data) {
                $.ajax({
                    url: 'http://127.0.0.1:8080/images/insert',
                    data: JSON.stringify(data.field),
                    contentType: "application/json;charset=utf-8",
                    dataType: 'json',
                    type: "post",
                    async: false,//false为同步，这个Ajax请求将整个浏览器锁死
                    success: function (d) {
                        if (d.msg == 1) {
                            layer.msg("添加成功", {icon: 6});
                            // window.parent.location.reload();//修改成功后刷新父界面

                            window.location.replace("table.html");
                        } else {
                            layer.msg("添加失败", {icon: 5});
                        }
                    }
                })


                var iframeIndex = parent.layer.getFrameIndex(window.name);
                parent.layer.close(iframeIndex);
                // var index = layer.alert(JSON.stringify(data.field), {
                //     title: '最终的提交信息'
                // }, function () {
                //
                //     // 关闭弹出层
                //     // layer.close(index);
                //
                //
                // });

                return false;
            });
        });





    })
</script>
</body>
</html>